<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>What If</title>
		<link href="CSS/style2.css" rel="stylesheet" type="text/css" />
        <script language="JavaScript" type="text/javascript">
        	var test;
        	var i;
        	
        	function UPP(ime){ //URL Parameter Parser
            	try{
					ime = ime.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
					var regexS = "[\\?&]"+ime+"=([^&#]*)";
					var regex = new RegExp( regexS );
					var results = regex.exec( window.location.href );
					if( results == null )
						return "";
					else
	        	    	return results[1];
	        	}catch(ex){
		            alert(ex);
		        }
        	}

        	function init(){
        		var Utez = '<option value="1">1</option>'+
        				   '<option value="2">2</option>'+
        				   '<option value="3">3</option>'+
        				   '<option value="4">4</option>'+
        				   '<option value="5">5</option>'+
        				   '<option value="6">6</option>'+
        				   '<option value="7">7</option>'+
        				   '<option value="8">8</option>'+
        				   '<option value="9">9</option>'+
        				   '<option value="10">10</option>';
        		try {
					//size printing
					var stPar = UPP("stPar");
					if((100*stPar) < 300)
						document.getElementById("mainTable").width = 350;
					else
						document.getElementById("mainTable").width = (100*stPar)+50;
					var alt1 = document.getElementById("alt1izpis");
					alt1.innerHTML = UPP("alt1");
        			var alt2 = document.getElementById("alt2izpis");
					alt2.innerHTML = UPP("alt2");
	    			test = document.getElementById("tabelaUtezi");
	        		var i;
	        		for(i=0; i < UPP("stPar"); i++){
	   		        	novaVrstica = test.insertRow(i+1);
	   		            novaCelica = novaVrstica.insertCell(0);
	   		            novaCelica.innerHTML = UPP("par"+i);
	   		            novaCelica = novaVrstica.insertCell(1);
	   		            novaCelica.innerHTML = UPP("utez"+i);
	   		            novaCelica = novaVrstica.insertCell(2);
	   		            novaCelica.innerHTML = '<select onChange="izracunaj()" id="alt1Utez'+i+'">' + Utez;
	   		            novaCelica = novaVrstica.insertCell(3);
	   		            novaCelica.innerHTML = '<select onChange="izracunaj()" id="alt2Utez'+i+'">' + Utez;
	        		} 
	        		izracunaj();
        		}catch(ex){
		            alert(ex);
		        }
        	}

        	function izracunaj(){
        		var alt1 = document.getElementById("alt1");
        		var alt2 = document.getElementById("alt2");
        		var izracun1 = document.getElementById("izracun1");
        		var izracun2 = document.getElementById("izracun2");
        		
				var temp1=0;
				var temp2=0;
        		
        		for(i=0; i < UPP("stPar"); i++){
        			var alt1Utez = document.getElementById("alt1Utez"+i);
            		var alt2Utez = document.getElementById("alt2Utez"+i);
					temp1 += UPP("utez"+i) * alt1Utez.value;
					temp2 += UPP("utez"+i) * alt2Utez.value; 
        		}
        		alt1.innerHTML = UPP("alt1");
        		alt2.innerHTML = UPP("alt2");
        		izracun1.innerHTML = temp1;
        		izracun2.innerHTML = temp2;
        		izrisiGrafCrtni();
        		izrisiGrafTortni(temp1,temp2);
            }

            function izrisiGrafCrtni(){
                var stPar = UPP("stPar");
                var link = "http://chart.apis.google.com/chart?"; //main link
                var cht = "cht=lc"; //type
                var chco = "&chco=FF0000,00FF00"; //colors
                var chs = "&chs="; //size
                var chd = "&chd=t:"; //data
                var chxt = "&chxt=x,y"; //axses
                var chxr = "&chxr="; //range
                var chds = "&chds=1,10"; //scale
                var chxl = "&chxl="; //label
                var chdl = "&chdl="; //legend
				var temp1;
				var temp2;

				//size printing
				if((100*stPar) < 300)
					chs = "&chs=300x200";
				else
					chs = "&chs="+(100*stPar)+"x200";

				//data printing
				temp1 = "";
				for(i=0; i < stPar; i++){
					var alt1Utez = document.getElementById("alt1Utez"+i);
					if((i+1) == stPar)
						temp1 += alt1Utez.value;
					else
						temp1 += alt1Utez.value+",";
				}
				temp2 = "";
				for(i=0; i < stPar; i++){
					var alt2Utez = document.getElementById("alt2Utez"+i);
					if((i+1) == stPar)
						temp2 += alt2Utez.value;
					else
						temp2 += alt2Utez.value+",";
				}
				chd = "&chd=t:"+temp1+"|"+temp2;
                
				//range printing				
				chxr = "&chxr=0,1,"+stPar+"|1,1,10";
				
				//label printing
				temp1 = "";
				for(i=0; i < stPar; i++){
					temp1 += "|" + UPP("par"+i);				
				}
				chxl = "&chxl=0:"+temp1+"|1:|1|2|3|4|5|6|7|8|9|10";

				//legent printing
				temp1 = "";
				for(i=0; i < 2; i++){
					if((i+1) == 2)
						temp1 += UPP("alt"+(i+1));
					else
						temp1 += UPP("alt"+(i+1)) + "|";
					
				}
				chdl = "&chdl="+temp1;

				//complete link
            	document.getElementById("googleGrafCrtni").src = link+cht+chco+chs+chd+chxt+chxr+chds+chxl+chdl;
            }

            function izrisiGrafTortni(vrednost1,vrednost2){
                var stPar = UPP("stPar");
                var link = "http://chart.apis.google.com/chart?"; //main link
                var cht = "&cht=p"; //type
                var chs = "chs="; //size
                var chd = "&chd=t:"; //data
                var chl = "&chl="; //label
                var temp1;
                var temp2;
				
				//size printing
				if((100*stPar) < 300)
					chs = "&chs=300x200";
				else
					chs = "&chs="+(100*stPar)+"x200";

				//data printing
				temp1 = ((vrednost1/(vrednost1+vrednost2))*100);
				temp2 = ((vrednost2/(vrednost1+vrednost2))*100);
				chd = "&chd=t:"+temp1+","+temp2;
                
				//label printing
				temp1 = UPP("alt1");
				temp2 = UPP("alt2");
				chl = "&chl="+temp1+"|"+temp2;

				//complete link
            	document.getElementById("googleGrafTortni").src = link+chs+chd+cht+chl;
            }
		</script>
    </head>
  
    <body onLoad="init()" >
        <div class="main" align="center">
           	<table id="mainTable" border="0" align="center">
				<tr>
					<th><h1>What If</h1></th>
				</tr>
				<tr>
                	<td align="center">
                    	<br />
                        <table id="tabelaUtezi" cellpadding="5">
                            <tr id="parameter">
                            	<th>Parameter</th>
                                <th>Utež</th>
                                <th id="alt1izpis"></th>
                                <th id="alt2izpis"></th>
                            </tr>
                        </table>
                        <br /><br />
                        <b>Črtni graf:</b><br />
                        <img id="googleGrafCrtni" src="" />
                        <br /><br />
                        <table cellpadding="5">
                            <tr>
                                <th>Alternativa:</th>
                                <th>Rezultat:</th>
                            </tr>
                            <tr>
                                <td id="alt1" ></td>
                                <td id="izracun1" ></td>
                            </tr>
                            <tr>
                                <td id="alt2" ></td>
                                <td id="izracun2" ></td>
                            </tr>
                        </table>
                        <br />
                        <b>Tortni graf:</b><br />
                        <img id="googleGrafTortni" src="" />
                        <br /><br />
                    </td>
                </tr>
            </table>
        </div>
    </body>
</html>